/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_gtk.h"

#define G_OBJECT_TAG_NAME__ZOBJECTS_GTK_GUIIMAGE_FILENAME	"zobjects_gtk:guiimage:filename"

ZlangInvokeFunction ZlangInvokeFunction_guiimage_Create_guiwindow;
int ZlangInvokeFunction_guiimage_Create_guiwindow( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangDirectProperty_guiimage		*guiimage_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guiimage_common = & (guiimage_direct_prop->common) ;

	EVAL_GTK_WIDGET_NEW_IN_CREATE( guiimage_common->gtk_widget , gtk_image_new() )
	SET_CONTROL_DEFAULT_STYLE( in1 , guiimage_common->gtk_widget )

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guiimage_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiimage_Create_guiwindow_string;
int ZlangInvokeFunction_guiimage_Create_guiwindow_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*filename = NULL ;
	struct ZlangDirectProperty_guiimage		*guiimage_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guiimage_common = & (guiimage_direct_prop->common) ;

	CallRuntimeFunction_string_GetStringValue( rt , in2 , & filename , NULL );

	EVAL_GTK_WIDGET_NEW_IN_CREATE( guiimage_common->gtk_widget , gtk_image_new_from_file(filename) )
	SET_CONTROL_DEFAULT_STYLE( in1 , guiimage_common->gtk_widget )
	g_object_set_data( G_OBJECT(guiimage_direct_prop->common.gtk_widget) , G_OBJECT_TAG_NAME__ZOBJECTS_GTK_GUIIMAGE_FILENAME , filename );

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiimage_LoadImageFile_string;
int ZlangInvokeFunction_guiimage_LoadImageFile_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*filename = NULL ;
	struct ZlangDirectProperty_guiimage		*guiimage_direct_prop = GetObjectDirectProperty(obj) ;

	CallRuntimeFunction_string_GetStringValue( rt , in1 , & filename , NULL );

	gtk_image_set_from_file( GTK_IMAGE(guiimage_direct_prop->common.gtk_widget) , filename );
	g_object_set_data( G_OBJECT(guiimage_direct_prop->common.gtk_widget) , G_OBJECT_TAG_NAME__ZOBJECTS_GTK_GUIIMAGE_FILENAME , filename );

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiimage_LoadIcon_string;
int ZlangInvokeFunction_guiimage_LoadIcon_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*icon_name = NULL ;
	struct ZlangDirectProperty_guiimage		*guiimage_direct_prop = GetObjectDirectProperty(obj) ;

	CallRuntimeFunction_string_GetStringValue( rt , in1 , & icon_name , NULL );

	gtk_image_set_from_icon_name( GTK_IMAGE(guiimage_direct_prop->common.gtk_widget) , icon_name );

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiimage_SetOriginalSize;
int ZlangInvokeFunction_guiimage_SetOriginalSize( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	const char					*filename = NULL ;
	GdkPixbuf					*pixbuf = NULL ;
	GError						*error = NULL ;
	int						width ;
	int						height ;
	struct ZlangDirectProperty_guiimage		*guiimage_direct_prop = GetObjectDirectProperty(obj) ;

	filename = g_object_get_data( G_OBJECT(guiimage_direct_prop->common.gtk_widget) , G_OBJECT_TAG_NAME__ZOBJECTS_GTK_GUIIMAGE_FILENAME );
	if( filename == NULL )
	{
		return 0;
	}

	pixbuf = gdk_pixbuf_new_from_file( filename , & error );
	width = gdk_pixbuf_get_width( pixbuf );
	height = gdk_pixbuf_get_height( pixbuf );
	g_object_unref( pixbuf );
	gtk_widget_set_size_request( guiimage_direct_prop->common.gtk_widget , width , height );

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZLANG_INVOKE_FUNCTION_guiwidget_all_common_functions( image )

ZLANG_INVOKE_FUNCTION_guictl_all_common_functions( image )

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_guiimage;
void *ZlangCreateDirectProperty_guiimage( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiimage		*guiimage_prop = NULL ;
	struct ZlangDirectProperty_guiwidget_common	*guiimage_common = NULL ;
	
	guiimage_prop = (struct ZlangDirectProperty_guiimage *)ZLMALLOC( sizeof(struct ZlangDirectProperty_guiimage) ) ;
	if( guiimage_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( guiimage_prop , 0x00 , sizeof(struct ZlangDirectProperty_guiimage) );

	guiimage_common = (struct ZlangDirectProperty_guiwidget_common *)guiimage_prop ;
	guiimage_common->gtk_widget_type_id = GTK_TYPE_IMAGE ;

	INIT_ZLANGDIRECTPROPERTY_guiwidget_common(guiimage)
	
	return guiimage_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_guiimage;
void ZlangDestroyDirectProperty_guiimage( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiimage	*guiimage_direct_prop = GetObjectDirectProperty(obj) ;
	
	CLEAN_ZLANGDIRECTPROPERTY_guiwidget_common(guiimage)

	ZLFREE( guiimage_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_guiimage;
void ZlangSummarizeDirectPropertySize_guiimage( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_guiimage) )
	return;
}

static struct ZlangDirectFunctions direct_funcs_guiimage =
	{
		ZLANG_OBJECT_guiimage , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_guiimage , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_guiimage , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		ZlangSummarizeDirectPropertySize_guiimage , /* ZlangSummarizeDirectPropertySizeFunction *summarize_direct_prop_size_func */
	} ;

ZlangImportObjectFunction ZlangImportObject_guiimage;
struct ZlangObject *ZlangImportObject_guiimage( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_guiimage , & direct_funcs_guiimage , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiimage_Create_guiwindow , ZLANG_OBJECT_guiimage , "Create" , ZLANG_OBJECT_guiwindow,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiimage_Create_guiwindow_string , ZLANG_OBJECT_guiimage , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiimage_LoadImageFile_string , ZLANG_OBJECT_guiimage , "LoadImageFile" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiimage_LoadIcon_string , ZLANG_OBJECT_guiimage , "LoadIcon" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiimage_SetOriginalSize , ZLANG_OBJECT_guiimage , "SetOriginalSize" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guiwidget_all_common_functions( image )

	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guictl_all_common_functions( image )

	return obj ;
}

